﻿using System.Collections.Generic;
using System.Linq;
using Company.Merchandising.BusinessLayer.DataEntities;
using Company.Merchandising.BusinessLayer.DataTransferObjects;

namespace Company.Merchandising.DataAccessLayer.DataAccessCommon
{
	public class DACFaculty : DACBase
	{
		public DACFaculty(MerchModelEntities modelEntities) : base(modelEntities) { }

		public int GetNumberOfFaculties()
		{
			return _modelEntities.Faculty.Count();
		}

		public void AddFaculty(DTOFaculty dtoFaculty)
		{
			AddFaculty(dtoFaculty, true);
		}

		public void AddFaculties(IList<DTOFaculty> faculties)
		{
			foreach (var faculty in faculties)
			{
				AddFaculty(faculty, false);
			}

			_modelEntities.SaveChanges();
		}

		public Faculty GetFacultyById(int facultyId)
		{
			return (from fac in _modelEntities.Faculty where fac.Id == facultyId select fac).First();
		}

		private void AddFaculty(DTOFaculty dtoFaculty, bool saveChanges)
		{
			Faculty faculty = new Faculty();
			faculty.Name = dtoFaculty.Name;
			faculty.Description = dtoFaculty.Description;

			_modelEntities.AddObject(faculty.GetType().Name, faculty);
			if (saveChanges)
			{
				_modelEntities.SaveChanges();
			}
		}
	}
}
